<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="Shortcut Icon" href="../images/aecc.ico" type="image/x-icon" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="../StaticFiles/element-ui/element-ui.css">
  <link rel="stylesheet" href="../StaticFiles/nprogress/nprogress.min.css">
  <link rel="stylesheet" href="../styles/vue-page-common.css">
  <script src="../Scripts/vue.min.js"></script>
  <script src="../StaticFiles/element-ui/element-ui.js"></script>
  <script src="../Scripts/axios.min.js"></script>
  <script src="../StaticFiles/nprogress/nprogress.min.js"></script>
  <script src="../Scripts/dayjs.min.js"></script>
  <script src="../Scripts/crypto-js.js"></script>
  <script src="../Scripts/secret.js"></script>
  <script src="../Scripts/axios.config.js"></script>
  <script src="../Scripts/datav.min.vue.js"></script>
  <script src="../Scripts/lodash.min.js"></script>
  <title>转工任务完成情况看板</title>
  <style>
    html {
      background-image: url(../images/bg.jpg);
      color: #fff;
    }

    .el-form-item__label {
      color: #fff;
    }

    .header-title {
      font-size: 24px !important;
    }

    .el-form-item {
      margin-bottom: 0 !important;
    }

    .chart-title {
      text-align: center;
      padding-top: 10px;
      color: #fff;
    }

    .chart {
      width: 100%;
      height: 80px;
      font-size: 40px;
      font-weight: bold;
      cursor: pointer;
      text-align: center;
    }
  </style>
</head>

<body>
  <div id="app" v-cloak>
    <div style="padding:0 5px;">
      <div class="parallelogram header-outer">
        <el-row>
          <el-col :span="7" style="padding-left: 35px;">
            <img src="../images/aeec_logo.png" height="60" alt="黎阳LOGO" />
          </el-col>
          <el-col :span="10" class="header-title">
            <div class="parallelogram header-inner">
              {{queryInfo.factoryname}}转工任务完成情况看板
            </div>
          </el-col>
          <el-col :span="7" class="currentTime" style="padding-right: 35px;font-size: 18px;">
            {{currentTime}}
          </el-col>
        </el-row>
      </div>

      <!-- 查询条件区 -->
      <el-form :inline="true" :model="queryInfo" ref="queryInfoRef" :label-position="'left'" :rules="rules"
        style="margin:10px 10px 0 10px;">
        <el-form-item label="转工要求完成时间:" prop="needcompletedate">
          <el-date-picker v-model="queryInfo.needcompletedate" type="daterange" range-separator="至"
            value-format="yyyy-MM-dd" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="searchData">查询</el-button>
        </el-form-item>
      </el-form>


      <el-row :gutter="10">
        <el-col :span="7">
          <dv-border-box-10>
            <h3 class="chart-title">转工任务合计</h3>
            <div class="chart" :style="{height: chartHeight + 'px'}" id="totalChart"></div>
          </dv-border-box-10>
        </el-col>
        <el-col :span="7">
          <dv-border-box-10>
            <h3 class="chart-title">待处理转工任务</h3>
            <div class="chart" :style="{height: chartHeight + 'px'}" id="notFinishBar"></div>
          </dv-border-box-10>
        </el-col>
        <el-col :span="10">
          <dv-border-box-10>
            <h3 class="chart-title">待处理转工任务分布</h3>
            <div class="chart" :style="{height: chartHeight + 'px'}" id="notFinishPie"></div>
          </dv-border-box-10>
        </el-col>
      </el-row>

      <dv-border-box-10>
        <h3 class="chart-title">各分厂转工任务情况</h3>
        <div class="chart" :style="{height: chartHeight + 'px'}" id="factoryBar"></div>
      </dv-border-box-10>
    </div>
  </div>
  <script src="../Scripts/echarts/echarts-5.4.0.min.js"></script>
  <script type="module">
    import { barOption, colorList } from '../Scripts/hooks/chartConfig.js'
    import { exportExcelData, getParams } from '../Scripts/hooks/useCommonMethod.js'
    import { getFactory } from '../Scripts/hooks/fetchAPI.js'
    import { prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth } from '../Scripts/hooks/useDateShortCut.js'
    const app = new Vue({
      el: '#app',
      data: {
        currentTime: '',
        loading: false,
        loadingText: '数据加载中...',
        queryInfo: { factoryid: '', factoryname: '', needcompletedate: '' },
        chartLoadingConfig: { text: '数据查询中...', effect: 'whirling', maskColor: 'transparent', textColor: '#FFF' },
        totalChartKey: '',
        rules: { needcompletedate: [{ required: true, message: '转工要求完成时间不能为空', trigger: 'change' }] },
        pickerOptions: { shortcuts: [prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth] },
        chartHeight: (window.innerHeight - 270) / 2
      },
      methods: {
        searchData() {
          this.$refs['queryInfoRef'].validate(async (valid) => {
            if (!valid) { return this.$message.error('必填项不能为空'); }
            this.getTotalBar();
            this.getNotCompleteData();
            this.getFactoryBar();
          })
        },
        async getTotalBar() {
          const dom = document.getElementById("totalChart");
          echarts.dispose(dom);
          let chart = echarts.init(dom);
          chart.showLoading(this.chartLoadingConfig);

          const { data: { data, meta, total } } = await axios.post('../handler/board/RbShipFinishInfoDashBoard.ashx?action=getTotalBar', {
            type: 'getTotalBar',
            ...this.queryInfo
          })
          chart.hideLoading();
          if (meta.status == 500) { return this.$message.error(meta.msg); }
          if (data.length === 0) { return; }

          echarts.dispose(dom);
          chart = echarts.init(dom);

          let arr_completerate = [], arr_intimerate = [], arr_delaydayssum = [];
          data.forEach(i => {
            arr_completerate.push(i.completerate);//完成率
            arr_intimerate.push(i.intimerate);//按时完成率
            arr_delaydayssum.push(i.delaydayssum);//超期天数
          });

          let option = barOption();

          option.grid = { left: '10%', right: '10%', bottom: '5%', containLabel: true };
          option.yAxis[0].axisLabel.formatter = function (data) { return data + "%"; };//y轴文字追加%
          option.yAxis[1].axisLabel.formatter = function (data) { return data };//y轴文字追加%

          option.legend.data = ['完成率', '按时完成率', '超期天数'];//设置图例文字
          option.xAxis.data = [''];

          option.series[0] = _.merge(option.series[0], {
            name: '完成率',
            data: arr_completerate,
            barMaxWidth: 35,
            barGap: '70%',
            label: { color: colorList.blue.color, formatter: '{c}%' },
            itemStyle: { color: { colorStops: colorList.blue.colorStops } }
          });

          option.series[1] = _.merge(_.cloneDeep(option.series[0]), {
            name: '按时完成率',
            data: arr_intimerate,
            label: { color: colorList.green.color },
            itemStyle: { color: { colorStops: colorList.green.colorStops } }
          });

          option.series[2] = _.merge(_.cloneDeep(option.series[0]), {
            name: '超期天数',
            data: arr_delaydayssum,
            yAxisIndex: 1,
            label: { color: colorList.red.color, formatter: '{c}天' },
            itemStyle: { color: { colorStops: colorList.red.colorStops } }
          });

          chart.setOption(option);

          window.addEventListener('resize', () => {
            chart.resize();
          });
        },
        async getNotCompleteData() {
          echarts.dispose(document.getElementById("notFinishBar"));
          echarts.dispose(document.getElementById("notFinishPie"));
          let chart = echarts.init(document.getElementById('notFinishBar'));
          let pieChart = echarts.init(document.getElementById('notFinishPie'));
          chart.showLoading(this.chartLoadingConfig);
          pieChart.showLoading(this.chartLoadingConfig);

          const { data: { data, meta, total } } = await axios.post('../handler/board/RbShipFinishInfoDashBoard.ashx?action=getNotCompleteData', {
            type: 'getNotCompleteData',
            ...this.queryInfo
          })
          chart.hideLoading();
          pieChart.hideLoading();
          if (meta.status == 500) { return this.$message.error(meta.msg); }
          if (data.length === 0) { return; }

          let totalTask = data.reduceRight((sum, current) => sum + current.taskcount, 0);
          let totalNotReceiveCount = data.reduceRight((sum, current) => sum + current.notreceivecount, 0);

          echarts.dispose(document.getElementById("notFinishBar"));
          echarts.dispose(document.getElementById("notFinishPie"));
          chart = echarts.init(document.getElementById('notFinishBar'));
          pieChart = echarts.init(document.getElementById('notFinishPie'));

          let option = barOption();
          option.grid = { left: '10%', right: '10%', bottom: '5%', containLabel: true };
          option.legend.show = false;
          option.xAxis.data = ['待接收', '待完成'];
          option.series[0].name = "完成率";
          option.series[0].data = [totalNotReceiveCount, totalTask];
          option.series[0].label.color = colorList.blue.color;
          option.series[0].itemStyle.color.colorStops = colorList.blue.colorStops;
          chart.setOption(option);

          window.addEventListener('resize', () => {
            chart.resize();
          });

          const arrFactory = [], arrTaskQty = [], arrNotReciveQty = [];
          for (const i of data) {
            arrFactory.push(i.factoryname);
            arrTaskQty.push(i.taskcount);
            arrNotReciveQty.push(i.notreceivecount);
          }

          let pieOption = barOption();
          pieOption.legend.data = ['待接收', '待完成'];//设置图例文字
          pieOption.xAxis.data = arrFactory;
          pieOption.yAxis[1].show = false;

          pieOption.series[0] = _.merge(pieOption.series[0], {
            name: '待接收',
            data: arrNotReciveQty,
            label: { color: colorList.blue.color },
            itemStyle: { color: { colorStops: colorList.blue.colorStops } }
          });

          pieOption.series[1] = _.merge(_.cloneDeep(pieOption.series[0]), {
            name: '待完成',
            data: arrTaskQty,
            label: { color: colorList.red.color },
            itemStyle: { color: { colorStops: colorList.red.colorStops } }
          });

          pieChart.setOption(pieOption);
          window.addEventListener('resize', () => {
            pieChart.resize();
          });
        },
        async getFactoryBar() {
          const dom = document.getElementById("factoryBar");
          echarts.dispose(dom);
          let chart = echarts.init(dom);
          chart.showLoading(this.chartLoadingConfig);

          const { data: { data, meta, total } } = await axios.post('../handler/board/RbShipFinishInfoDashBoard.ashx?action=getFactoryBar', {
            type: 'getFactoryBar',
            ...this.queryInfo
          })
          chart.hideLoading();
          if (meta.status == 500) { return this.$message.error(meta.msg); }
          if (data.length === 0) { return; }

          echarts.dispose(dom);
          chart = echarts.init(dom);

          let arr_xAxis = [], arr_completerate = [], arr_intimerate = [], arr_delaydayssum = [];
          for (const i of data) {
            arr_xAxis.push(i.factoryname);
            arr_completerate.push(i.completerate);//完成率
            arr_intimerate.push(i.intimerate);//按时完成率
            arr_delaydayssum.push(i.delaydayssum);//超期天数
          }

          let option = barOption();

          option.yAxis[0].axisLabel.formatter = function (data) { return data + "%"; };//y轴文字追加%
          option.yAxis[1].axisLabel.formatter = function (data) { return data };//y轴文字追加%

          option.legend.data = ['完成率', '按时完成率', '超期天数'];//设置图例文字
          option.xAxis.data = arr_xAxis;

          option.series[0] = _.merge(option.series[0], {
            name: '完成率',
            data: arr_completerate,
            barMaxWidth: 35,
            barGap: '70%',
            label: { color: colorList.blue.color, formatter: '{c}%' },
            itemStyle: { color: { colorStops: colorList.blue.colorStops } }
          });

          option.series[1] = _.merge(_.cloneDeep(option.series[0]), {
            name: '按时完成率',
            data: arr_intimerate,
            label: { color: colorList.green.color },
            itemStyle: { color: { colorStops: colorList.green.colorStops } }
          });

          option.series[2] = _.merge(_.cloneDeep(option.series[0]), {
            name: '超期天数',
            data: arr_delaydayssum,
            yAxisIndex: 1,
            label: { color: colorList.red.color, formatter: '{c}天' },
            itemStyle: { color: { colorStops: colorList.red.colorStops } }
          });

          chart.setOption(option);

          window.addEventListener('resize', () => {
            chart.resize();
          });
        }
      },
      async mounted() {
        window.addEventListener('resize', () => {
          this.chartHeight = (window.innerHeight - 270) / 2
        });
        let incrementNumber = () => {
          setTimeout(incrementNumber, 1000);
          const weekDayText = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][dayjs().format('d')];
          this.currentTime = `${dayjs().format('YYYY年MM月DD日')} ${weekDayText} ${dayjs().format('HH:mm')}`;
        }
        setTimeout(incrementNumber, 500);
        const startOfThisMonth = dayjs().startOf('month').format('YYYY-MM-DD');
        const endOfThisMonth = dayjs().endOf('month').format('YYYY-MM-DD');
        this.queryInfo.needcompletedate = [startOfThisMonth, endOfThisMonth];
        const factoryid = Decrypt(getParams('factoryid'));
        this.queryInfo.factoryid = factoryid;
        const factoryInfo = await getFactory({ factoryid });
        if (factoryInfo) {
          const { factoryname } = factoryInfo[0];
          this.queryInfo.factoryname = factoryname;
          document.title = `${factoryname}转工任务完成情况看板`;
        }
        this.searchData();
      }
    })
  </script>
</body>

</html>